Natctl : Network address translation
This module provides the function of network address translation, which can convert the internal network address into a legal public network address.
User can use the following code to import the natctl
module.
var natctl = require('router/natctl');
Support
The following shows natctl
module APIs available for each permissions.
User Mode | Privilege Mode | |
---|---|---|
natctl.start | ● | |
natctl.stop | ● | |
natctl.ifAdd | ● | |
natctl.ifDelete | ● | |
natctl.ifCount | ● | |
natctl.mapAdd | ● | |
natctl.mapGet | ● | |
natctl.mapDelete | ● | |
natctl.fragment | ● | |
natctl.sessions | ||
natctl.trafficStart | ● | |
natctl.trafficStop | ● | |
natctl.trafficIsStart | ● | |
natctl.traffic | ● |
Natctl Object
natctl.start()
Enable network address translation. At least one WAN port and one LAN port are added. LAN can be a real network interface or a bridge interface.
natctl.stop()
Stop network address translation.
natctl.ifAdd(ifname, isLAN)
ifname
{String} Network interface name.isLAN
{Boolean} Whether it is a LAN interface.- Returns: {Boolean} Whether the operation was successful.
Add a network interface to a NAT-managed interface. NAT Manager supports multiple WAN ports and multiple LAN ports.
Example
natctl.ifAdd('pp5', false); // Add WAN port
natctl.ifAdd('en1', true); // Add LAN port
natctl.ifAdd('en2', true); // Add LAN port
natctl.start(); // Start NAT
natctl.ifDelete([ifname])
ifname
{String} Network interface name. default: undefined means all interface.- Returns: {Boolean} Whether the operation was successful.
Remove a network interface from the NAT manager. If NAT is running, it needs to be stopped before it can take effect.
Example
natctl.stop(); // Stop NAT
natctl.ifDelete('en1');
natctl.start(); // Start NAT
natctl.ifCount()
- Returns: {Object} Number of network interfaces.
Get NAT number of network interfaces, the returned object contains the following members:
lan
{Integer} Number of LAN network interfaces.wan
{Integer} Number of WAN network interfaces.
natctl.mapAdd(ipLocal, portLocal, portWan, proto[, ipCnt])
ipLocal
{String} Local IP address to be mapped.portLocal
{Integer} Local ports to be mapped.portWan
{Integer} WAN network port to be mapped.proto
{Integer} TCP or UDP protocol.ipCnt
{Integer} Consecutive intranet addresses mapped fromipLocal
address. Multi-machine equilibrium mapping. default: 1.- Returns: {Integer} Index number of this map rule.
Create a mapping so that access to an address on the WAN port is mapped to the specified intranet address.
Example
// Map TCP:WAN:80 -> to -> TCP:10.0.0.2:8000
natctl.mapAdd('10.0.0.2', 8000, 80, natctl.TCP);
natctl.mapGet()
- Returns: {Array} List of all map rules added previously.
Each rule is an object in the array, which contains the following properties:
index
{Integer} Index number of this map rule.proto
{Integer} TCP or UDP protocol. (natctl.TCP
ornatctl.UDP
)portLocal
{Integer} Local ports to be mapped.portWan
{Integer} WAN network port to be mapped.ipCnt
{Integer} Consecutive intranet addresses mapped fromipLocal
address.ipLocal
{String} Local IP address to be mapped.
Example
var maps = natctl.mapGet();
maps.forEach(function(map) {
// ...
});
natctl.mapGet(index)
index
{Integer} Index number of this map rule.- Returns: {Object} Rule object corresponding to index.
Same as natctl.mapGet()
, but only get the rule specified by index.
natctl.mapDelete()
- Returns: {Boolean} Whether the operation was successful.
Delete all map rules added in this process.
Example
natctl.mapDelete();
natctl.mapDelete(index)
index
{Integer} Index number of this map rule.- Returns: {Object} Rule object corresponding to index.
Same as natctl.mapDelete()
, but only remove the rule specified by index.
natctl.fragment()
- Returns: {Object} Whether various protocol fragments are enabled.
Obtain whether the various protocol fragments are enabled.
The returned object includes the following properties:
tcp
{Boolean} Whether TCP segmentation of the NAT network is enabled.udp
{Boolean} Whether UDP segmentation of the NAT network is enabled.icmp
{Boolean} Whether ICMP segmentation of the NAT network is enabled.
natctl.fragment(opt)
opt
{Object} Various protocol fragments setting.- Returns: {Boolean} Whether the setup was successful.
Set up various protocol fragments setting for NAT network.
The opt
object can includes the following properties:
tcp
{Boolean} Whether TCP segmentation of the NAT network is enabled.udp
{Boolean} Whether UDP segmentation of the NAT network is enabled.icmp
{Boolean} Whether ICMP segmentation of the NAT network is enabled.
Example
natctl.fragment({ tcp: false, udp: true });
natctl.sessions()
- Returns: {Array} Statistics of the current NAT session.
Get statistics of the current NAT session, returns empty array if NAT is not started.
Each item in the returns array is an object, this object include the following properties:
ipaddr
{String} Intranet host IP address.sessions
{Integer} Number of currently held sessions.
Example
var array = natctl.sessions();
for (var i = 0; i < array.length; i++) {
console.log('IP:', array[i].ipaddr, 'Cnt:', array[i].sessions);
}
Natctl Traffic Statistics
This function is supported in EdgerOS 1.7.8 and later.
natctl.trafficStart()
Enable NAT network traffic statistics function.
natctl.trafficStop()
Disable NAT network traffic statistics function.
natctl.trafficIsStart()
- Returns: {Boolean} Enabled or not. Whether the NAT network traffic statistics function is enabled.
natctl.traffic()
- Returns: {Array} Statistics of traffic.
Each member of the array contains the following information:
ipaddr
{String} Intranet host IP address.orate
{Integer} Output rate of this node (Kbps).irate
{Integer} Input rate of this node (Kbps).ototal
{Integer} The amount of data sent by this node since the last start of statistics (KBytes).itotal
{Integer} The amount of data received by this node since the last start of statistics (KBytes).